package com.newland.mesdk.demo.interfaceimpl;

import java.util.concurrent.TimeUnit;

import com.newland.mtype.ModuleType;
import com.newland.mtype.event.DeviceEventListener;
import com.newland.mtype.module.common.externalPin.BaudrateType;
import com.newland.mtype.module.common.externalPin.DataBitType;
import com.newland.mtype.module.common.externalPin.ExtTransResult;
import com.newland.mtype.module.common.externalPin.ExternalPinInput;
import com.newland.mtype.module.common.externalPin.OddEvenCheckType;
import com.newland.mtype.module.common.pin.AccountInputType;
import com.newland.mtype.module.common.pin.EncryptType;
import com.newland.mtype.module.common.pin.KeyManageType;
import com.newland.mtype.module.common.pin.MacAlgorithm;
import com.newland.mtype.module.common.pin.PinInputEvent;
import com.newland.mtype.module.common.pin.StopBitType;
import com.newland.mtype.module.common.pin.WorkingKey;
import com.newland.mtype.module.common.pin.WorkingKeyType;
import com.newland.mesdk.demo.moduleinterface.ExternalPinInputInterface;
import com.newland.mesdk.demo.util.ModuleBase;

/**
 * Created by YJF 外接密码键盘接口实现类
 */
public class ExternalPinInputInterfaceImpl extends ModuleBase implements ExternalPinInputInterface {
	private ExternalPinInput externalPinInput;

	public ExternalPinInputInterfaceImpl() {
		externalPinInput = (ExternalPinInput) factory.getModule(ModuleType.EXTERNAL_PININPUT);
	}

	// mac计算
	@Override
	public byte[] calcMac(MacAlgorithm macAlgorithm, WorkingKey wk, byte[] input, int mainKeyIndex) {
		byte[] calacMac = externalPinInput.calcMac(macAlgorithm, wk, input, mainKeyIndex);
		return calacMac;
	}

	// 取消密码输入
	@Override
	public void cancelPinInput() {
		externalPinInput.cancelPinInput();
	}

	// 解密一串数据
	@Override
	public byte[] decrypt(WorkingKey wk, EncryptType encryptType, byte[] input, byte[] cbcInit, int mainkeyIndex) {
		byte[] decrypt = externalPinInput.decrypt(wk, encryptType, input, cbcInit, mainkeyIndex);
		return decrypt;
	}

	// 加密一串数据
	@Override
	public byte[] encrypt(WorkingKey wk, EncryptType encryptType, byte[] input, byte[] cbcInit, int mainkeyIndex) {
		byte[] encrypt = externalPinInput.encrypt(wk, encryptType, input, cbcInit, mainkeyIndex);
		return encrypt;
	}

	// 数据透传
	@Override
	public ExtTransResult extKeyboardUnvTransmission(int respDataLength, int timeout, TimeUnit timeUnit, byte[] data) {
		ExtTransResult extTransResult = externalPinInput.extKeyboardUnvTransmission(respDataLength, timeout, timeUnit,
				data);
		return extTransResult;
	}

	// 装载主密钥
	@Override
	public byte[] loadMainKey(int mainIndex, byte[] data) {
		byte[] loadMainKey = externalPinInput.loadMainKey(mainIndex, data);
		return loadMainKey;
	}

	// 装载一个工作密钥
	@Override
	public byte[] loadWorkingKey(WorkingKeyType type, int mainKeyIndex, int workingKeyIndex, byte[] data) {
		byte[] loadWorkingKey = externalPinInput.loadWorkingKey(type, mainKeyIndex, workingKeyIndex, data);
		return loadWorkingKey;
	}

	// pinpad口初始化
	@Override
	public void pinPadPortInit(BaudrateType baudrate, DataBitType dataBit, OddEvenCheckType oddEvenCheck,
			StopBitType stopBit) {
		externalPinInput.pinPadPortInit(baudrate, dataBit, oddEvenCheck, stopBit);
	}

	// 开启一个密码输入过程
	@Override
	public PinInputEvent startExternalPinInput(WorkingKey workingKey, KeyManageType keyManageType,
			AccountInputType acctInputType, String acctSymbol, int inputMaxLen, byte[] pinPadding,
			boolean isEnterEnabled, long timeout, TimeUnit timeunit, int mainKeyIndex) {
		PinInputEvent pinInputEvent = externalPinInput.startExternalPinInput(workingKey, keyManageType, acctInputType,
				acctSymbol, inputMaxLen, pinPadding, isEnterEnabled, timeout, timeunit, mainKeyIndex);
		return pinInputEvent;
	}

	// 开启一个密码输入过程
	@Override
	public void startExternalPinInput(WorkingKey workingKey, KeyManageType keyManageType,
			AccountInputType acctInputType, String acctSymbol, int inputMaxLen, byte[] pinPadding,
			boolean isEnterEnabled, long timeout, TimeUnit timeunit, int mainKeyIndex,
			DeviceEventListener<PinInputEvent> inputListener) {
		externalPinInput.startExternalPinInput(workingKey, keyManageType, acctInputType, acctSymbol, inputMaxLen,
				pinPadding, isEnterEnabled, timeout, timeunit, mainKeyIndex, inputListener);
	}
}
